﻿package app {
	
	import flash.events.TouchEvent;
	import flash.events.TUIO;
	
	import app.core.action.RotatableScalable
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.text.*;
	import flash.text.engine.TypographicCase;
	import flash.display.Shape;
	import flash.utils.Dictionary;
	
	import app.LetterBtn;
	import app.ShiftBtn;
	import app.BigLetter;
	import app.BackSpaceBtn;
	import app.SpaceBtn;
	
	public class MainKey extends RotatableScalable
	{	
		public var Letter:LetterBtn; 							//from library
		public var Shift:ShiftBtn = new ShiftBtn();				//from library
		public var bkSpc:BackSpaceBtn = new BackSpaceBtn();		//from library
		public var Spc:SpaceBtn = new SpaceBtn();				//from library
		public var allLettersArray:Array = new Array();
		
		public var bigDisplay:BigLetter = new BigLetter();		//from library
		private var BigDispDef:Dictionary = new Dictionary(true); // to track which preview displays to add/remove
		
		public var numbersRow:String = "1234567890-";
		public var firstRow:String = "qwertyuiop";
		public var secondRow:String = "asdfghjkl";
		public var thirdRow:String = "zxcvbnm";
		public var extrasRow:String = ",.";
		public var letterArray:Array = new Array();
		
		public var typed:String = ""; // Value to enter into TextField
		public var attachedTextField:TextField = new TextField(); // dynamic TextField connection
		
		public function MainKey()
		{						
			TUIO.init(this,'127.0.0.1',3000,'',false);			
			initKeyboard();
		}
		
		public function initKeyboard():void{
			
			//-------- Generated Background oh Keyboard
			var child:Shape = new Shape();
            child.graphics.beginFill(0x222222);
            child.graphics.lineStyle(10, 0x999999);
            child.graphics.drawRoundRect(-15, -15, 495, 285, 10);
            child.graphics.endFill();
            addChild(child);
			
			//-------- Creates Rows of Keys from dynamic library template movie clip
			letterArray = numbersRow.split("");
			var arrayLength = letterArray.length;
			for(var n:int = 0; n < arrayLength; ++n){
				Letter = new LetterBtn(letterArray[n]);
				addChild(Letter);
				Letter.addEventListener(TouchEvent.MOUSE_DOWN, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_OVER, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_UP, selectLetter);
				Letter.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
				Letter.x = Letter.width * n - .25;
				allLettersArray.push(Letter);
			}
			
			letterArray = null;
			letterArray = firstRow.split("");
			arrayLength = letterArray.length;
			for(var i:int = 0; i < arrayLength; ++i){
				Letter = new LetterBtn(letterArray[i]);
				addChild(Letter);
				Letter.addEventListener(TouchEvent.MOUSE_DOWN, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_OVER, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_UP, selectLetter);
				Letter.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
				Letter.x = Letter.width * i + (Letter.width/2) - .25;
				Letter.y = Letter.height;
				allLettersArray.push(Letter);
			}
			
			letterArray = null;
			letterArray = secondRow.split("");
			arrayLength = letterArray.length;
			for(var z:int = 0; z < arrayLength; ++z){
				Letter = new LetterBtn(letterArray[z]);
				addChild(Letter);
				Letter.addEventListener(TouchEvent.MOUSE_DOWN, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_OVER, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_UP, selectLetter);
				Letter.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
				Letter.x = Letter.width * z + (Letter.width/2) + (Letter.width/2) - .25;
				Letter.y = Letter.height * 2;
				allLettersArray.push(Letter);
			}
			
			letterArray = null;
			letterArray = thirdRow.split("");
			arrayLength = letterArray.length;
			for(var c:int = 0; c < arrayLength; ++c){
				Letter = new LetterBtn(letterArray[c]);
				addChild(Letter);
				Letter.addEventListener(TouchEvent.MOUSE_DOWN, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_OVER, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_UP, selectLetter);
				Letter.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
				Letter.x = Letter.width * c + Letter.width + (Letter.width/2) - .25;
				Letter.y = Letter.height * 3;
				allLettersArray.push(Letter);
			}
			
			//-------- Applies special case letters, spacebar, '.' and ',', Backspace, Shift
			
			addChild(Spc);
			Spc.y = Letter.height * 4;
			Spc.x = Letter.width * 1 + (Letter.width/2) + (Letter.width/2) - .25;
			Spc.addEventListener(TouchEvent.MOUSE_DOWN, showPreview);
			Spc.addEventListener(TouchEvent.MOUSE_OVER, showPreview);
			Spc.addEventListener(TouchEvent.MOUSE_UP, Space);
			
			letterArray = null;
			letterArray = extrasRow.split("");
			arrayLength = letterArray.length;
			for(var ex:int = 0; ex < arrayLength; ++ex){
				Letter = new LetterBtn(letterArray[ex]);
				addChild(Letter);
				Letter.addEventListener(TouchEvent.MOUSE_DOWN, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_OVER, showPreview);
				Letter.addEventListener(TouchEvent.MOUSE_UP, selectLetter);
				Letter.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
				if(ex == 0){
					Letter.x = Letter.width * ex + Letter.width - .25;
				}else{
					Letter.x = Letter.width * ex + Letter.width + Spc.width - .25;
				}
				Letter.y = Letter.height * 4;
				allLettersArray.push(Letter);
			}
			
			addChild(bkSpc);
			bkSpc.y = Letter.height * 3;
			bkSpc.x = Letter.width * 8 + (Letter.width/2) - .25;
			bkSpc.fakeDelPrev.alpha = 0;
			bkSpc.fakeDelPrev.scaleX = 0;
			bkSpc.fakeDelPrev.scaleY = 0;
			bkSpc.addEventListener(TouchEvent.MOUSE_DOWN, showPreviewBak);
			bkSpc.addEventListener(TouchEvent.MOUSE_OVER, showPreviewBak);
			bkSpc.addEventListener(TouchEvent.MOUSE_UP, backSpace);
			bkSpc.addEventListener(TouchEvent.MOUSE_OUT, hidePreviewBak);
			bkSpc.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
			
			addChild(Shift);
			Shift.y = Letter.height * 3;
			Shift.x = Letter.width/2;
			Shift.fakeShiftPreview.alpha = 0;
			Shift.fakeShiftPreview.scaleX = 0;
			Shift.fakeShiftPreview.scaleY = 0;
			Shift.addEventListener(TouchEvent.MOUSE_DOWN, showPreviewShift);
			Shift.addEventListener(TouchEvent.MOUSE_OVER, showPreviewShift);
			Shift.addEventListener(TouchEvent.MOUSE_UP, changeShiftKey);
			Shift.addEventListener(TouchEvent.MOUSE_OUT, hidePreviewShift);
			Shift.addEventListener(TouchEvent.MOUSE_MOVE, noProp);
			
			
			this.addEventListener(TouchEvent.MOUSE_MOVE, noProp); //Applied to stop interaction of objects below the kayboard
			
		}
		
		//Applied to stop interaction of objects below the kayboard
		public function noProp(evt:TouchEvent):void{
			evt.stopPropagation();
		}
		
		//custom TouchEvent code to show/hide custom preview graphic which is embedded into key's movie clip
		public function showPreviewBak(evt:TouchEvent):void{
			bkSpc.fakeDelPrev.alpha = 1;
			bkSpc.fakeDelPrev.scaleX = 1;
			bkSpc.fakeDelPrev.scaleY = 1;
		}
		public function hidePreviewBak(evt:TouchEvent):void{
			bkSpc.fakeDelPrev.alpha = 0;
			bkSpc.fakeDelPrev.scaleX = 0;
			bkSpc.fakeDelPrev.scaleY = 0;
		}
		//Backspace Key Event
		public function backSpace(evt:TouchEvent):void{
			bkSpc.fakeDelPrev.alpha = 0;
			bkSpc.fakeDelPrev.scaleX = 0;
			bkSpc.fakeDelPrev.scaleY = 0;
			typed = typed.slice(0, -1);
			trace(typed);
			attachedTextField.text = typed;
			
		}
		
		//Space Key Event
		public function Space(evt:TouchEvent):void{
			typed = typed+" ";
			trace(typed);
			attachedTextField.text = typed;
			
		}
		

		//custom TouchEvent code to show/hide custom preview graphic which is embedded into key's movie clip
		public function showPreviewShift(evt:TouchEvent):void{
			Shift.fakeShiftPreview.alpha = 1;
			Shift.fakeShiftPreview.scaleX = 1;
			Shift.fakeShiftPreview.scaleY = 1;
		}
		public function hidePreviewShift(evt:TouchEvent):void{
			Shift.fakeShiftPreview.alpha = 0;
			Shift.fakeShiftPreview.scaleX = 0;
			Shift.fakeShiftPreview.scaleY = 0;
		}
		//Shift Key Event
		public function changeShiftKey(evt:TouchEvent):void{
			Shift.fakeShiftPreview.alpha = 0;
			Shift.fakeShiftPreview.scaleX = 0;
			Shift.fakeShiftPreview.scaleY = 0;
			var allLettersTotal:Number = allLettersArray.length;
			for(var f:Number = 0; f < allLettersTotal; ++f){
				allLettersArray[f].dispatchEvent(new Event("shiftChange"));
				trace("throw shift event " + allLettersArray[f].Letter);
			}
		}
				
				
		// Show/Hide Preview MovieClip dynamically
		public function showPreview(evt:TouchEvent):void{
			if(BigDispDef[evt.ID] == null){
				var thisObj:Object = evt.currentTarget;
				bigDisplay = new BigLetter(); 
				bigDisplay.BtnTxt.text = thisObj.Letter;
				bigDisplay.x = thisObj.x + (thisObj.width / 2);
				bigDisplay.y = thisObj.y + (thisObj.height / 2);
				addEventListener(TouchEvent.MOUSE_UP, removePreview);
				addEventListener(TouchEvent.MOUSE_OUT, removePreview);
				addChild(bigDisplay);
				BigDispDef[evt.ID] = bigDisplay;
				evt.stopPropagation();
			}
		}
		public function removePreview(evt:TouchEvent):void{
			if(BigDispDef[evt.ID] != null){
				var thisObj:Object = evt.ID;
				trace("remove: "+thisObj)
				removeChild(BigDispDef[evt.ID]);
				BigDispDef[evt.ID] = null;
			}
		}
		public function hidePreview(evt:TouchEvent):void{
			bigDisplay.x = 0;
			bigDisplay.y = 0;
			bigDisplay.scaleX = .001;
			bigDisplay.scaleY = .001;
			bigDisplay.alpha = 0;
		}
		
		
		// apply selected letter to attachedTextField TextField
		public function selectLetter(evt:TouchEvent):void{
			var thisObj:Object = evt.currentTarget;
			trace("SELECT " + thisObj.Letter);
			
			typed += thisObj.Letter;
			trace(typed);
			attachedTextField.text = typed;
			
			this.dispatchEvent(new Event("Letter"));
			if(isUpperCase(thisObj.Letter)){
				changeShiftKey(null);
			}
			
		}
		
		// Changes case once Shift is selected
		public function isUpperCase(pos:String):Boolean{
			var c = pos.charCodeAt(0);
			return (c > 64 && c < 91);
		}
		
		 
	}
}